<template>
  <div class="outer">
    <div style="display: inline-block;" class="logo" @click="toHome">
      <span style="color: #3a86ff">Blink</span>
    </div>
    <div class="login-container">
      <div class="login-banner">
        <img src="../assets/background_login.jpg" style="background-size: auto;">
      </div>
      <div class="login-content">
        <span class="Hello">Hello，</span>
        <span class="tips">欢迎使用Blink!</span>
        <div class="login-form-tab">
          <el-tabs v-model="activeName" class="login-top">
            <el-tab-pane label="账号密码登录" name="first">
              <el-form :model="ruleForm" :rules="rules" ref="loginRuleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item prop="userName">
                  <el-input prefix-icon="el-icon-user-solid" type="text" v-model="ruleForm.userName"
                    placeholder="请输入您的用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input prefix-icon="el-icon-lock" type="password" v-model="ruleForm.password"
                    placeholder="请输入您的密码"></el-input>
                </el-form-item>
                <el-form-item class="login-bottom">
                  <el-button type="primary" @click="login('ruleForm')">登录</el-button>
                  <el-button @click="register()">立即注册</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      activeName: "first",
      ruleForm: {
        userName: "",
        password: ""
      },
      rules: {
        userName: [
          { required: true, message: "", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 5 到 15 之间", trigger: "blur" }
        ],
        password: [
          { required: true, message: "", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 5 到 15 之间", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 跳转主页
    toHome() {
      this.$router.push({path: '/'});
    },
    register() {
      this.$router.replace("/register");
    },
    login() {
      if (this.$store.state.isLogin) {
        this.$message({
          message: '您已登录！',
          type: 'warning'
        })
        return;
      }
      this.$refs.loginRuleForm.validate(valid => {
        if (valid) {
          this.$serve
            .userLogin(this.ruleForm)
            .then(res => {
              if (res.data.success == 1) {
                this.$store.state.isLogin = true;
                this.$store.state.userName = this.ruleForm.userName;
                this.$message.success("登录成功");
                this.$router.replace("/manage");
              } else {
                this.$message.error("登录失败,请检查账号密码");
              }
            })
            .catch(err => {
              console.log("login", err);
              alert("网络错误");
            });
        }
      });

      // 还是先不要删掉，万一测试出问题还能改回来
      // this.$axios
      // .post("127.0.0.1/user/login/", {
      //   userName: ruleForm.userName,
      //   password: ruleForm.password
      // })
      // .then(res => {
      //   if (res.data.success == 1) {
      //     this.$store.state.isLogin=true;
      //     this.$store.state.userName=this.ruleForm.userName;
      //     alert("登录成功");
      //     this.$router.replace("/register");
      //   } else {
      //     console.log("账号或密码错误");
      //     return false;
      //   }
      // });
    }
  }
};
</script>
<style scoped>
.outer {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.login-container {
  margin-top: 60px;
  width: 100%;
  box-sizing: border-box;
  display: flex;
}
.login-banner {
  width: 50%;
  box-sizing: border-box;
}
.login-banner img {
  width: 100%;
}
.login-content {
  color: #25262b;
  margin-left: 100px;
}
.login-content span {
  font-size: 40px;
  font-weight: 700;
  color: #10141c;
  line-height: 134px;
}
.login-content .tips {
  font-size: 21px;
  margin-left: 10px;
}
.login-form-tab {
  width: 300px;
  box-sizing: border-box;
  /* background-color: red; */
  height: 245px;
}
/deep/ .el-form-item__content {
  margin: 0 !important;
}
.login-top {
}
/deep/ .el-tabs__item {
  font-size: 20px;
  font-weight: 500;
}
/deep/ .el-input {
  height: 39px !important;
}
/deep/ .el-input__inner {
  background-color: #f2f2f2 !important;
}
/deep/ .el-tabs__active-bar {
  width: 120px !important;
  height: 7px !important;
  border-radius: 4px !important;
  background-color: #d8d8d8 !important;
}
.login-bottom {
  margin-top: 30px;
  display: flex;
}
.logo {
  height: 60px;
  display: inline-block;
  line-height: 60px;
  font-size: 30px;
  font-weight: 750;
  position: absolute;
  left: 50px;
  cursor: pointer;
}
</style>